# -*- coding: utf-8 -*-
# @Time    : 2020/4/20 下午2:48
# @Author  : tsl
# @Email   : mymailwith163@163.com
# @File    : renameLabel.py
# @Software: PyCharm

import os

from detail import classID

def mkpath(path):
    if(os.path.exists(path)):
        return path
    os.mkdir(path)
    return path

def renameLabel(src,dst):
    idx = classID[src.split("/")[-1]]
    for file in os.listdir(src):
        if(file.endswith("txt") and not file.startswith("cl")):
            new_label = open(os.path.join(mkpath(os.path.join(dst,src.split("/")[-1])),file),'w')
            with open(os.path.join(src,file),'r') as f:
                lines = f.readlines()
                for line in lines:
                    line = line.rstrip().split(" ")
                    label,x,y,w,h = line
                    # if(label!="0"):
                    #     continue
                    new_label.write(" ".join([str(idx),x,y,w,h])+"\n")
            new_label.close()

def main():

    keys = list(classID.keys())

    for k in keys:
        print(k)
        if k != "11_车牌":
            # if k != "3_河岸施工行为":
            #     continue
            # if k != "18_倒地":
            #     continue
            src = os.path.join("/mnt/data/yunnan/labels/"+k)
            dst = "/mnt/data/yunnan/new_labels"
            renameLabel(src,dst)

if __name__ == '__main__':
    main()